package com.casstime.ec.cloud.meeting.web.controller;

import com.casstime.commons.model.Result;
import com.casstime.commons.service.exception.HttpMessageException;
import com.casstime.ec.cloud.meeting.values.BookDetailDTO;
import com.casstime.ec.cloud.meeting.values.BookDetailQuery;
import com.casstime.ec.cloud.meeting.values.BookMeetingRoomParam;
import com.casstime.ec.cloud.meeting.values.DepartmentDTO;
import com.casstime.ec.cloud.meeting.values.EquipDTO;
import com.casstime.ec.cloud.meeting.values.MeetingRoomDTO;
import com.casstime.ec.cloud.meeting.values.MeetingRoomSaveParam;
import com.casstime.ec.cloud.meeting.values.ResultBookMeetingRoom;
import com.casstime.ec.cloud.meeting.values.ResultDeleteMyBook;
import com.casstime.ec.cloud.meeting.values.UserDTO;
import com.casstime.ec.cloud.meeting.web.entity.ResultBookDetailDTO;
import com.casstime.ec.cloud.meeting.web.entity.ResultDTO;
import com.casstime.ec.cloud.meeting.web.entity.ResultRoomDTO;
import com.casstime.ec.cloud.meeting.web.entity.UserLoginDTO;
import com.casstime.ec.cloud.meeting.web.feignclient.MeetingRoomClient;
import io.swagger.annotations.Api;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Log4j2
@Controller
@Api
public class MeetingWebController {

  @Autowired MeetingRoomClient meetingRoomClient;

  @GetMapping("/index")
  public String html() {
    return "redirect:/static/index.html";
  }

  /**
   * * 获取供应商优惠券列表
   *
   * @param roomId
   * @return
   */
  @RequestMapping(value = "rooms", method = RequestMethod.GET)
  @ResponseBody
  public Result<MeetingRoomDTO> roomDetail(@RequestParam("roomId") Integer roomId) {
    MeetingRoomDTO roomDTO = null;
    try {
      roomDTO = meetingRoomClient.getMeetingRoomById(roomId);
    } catch (Exception e) {
      log.error("会议室详情查询失败，会议室编码为：{}", roomId, e);
      Result.error("服务器错误");
    }
    return Result.success(roomDTO);
  }

  /**
   * * 登录
   *
   * @param user
   * @return
   */
  @RequestMapping(value = "/login", method = RequestMethod.POST)
  @ResponseBody
  public UserLoginDTO login(@RequestBody UserDTO user,HttpServletRequest request) {
    UserLoginDTO loginDTO = new UserLoginDTO();
    if (user.getUsername() == null) {
      loginDTO.setCode(400);
      loginDTO.setMsg("用户名不能为空");
      return loginDTO;
    } else if(user.getPassword() == null) {
      loginDTO.setCode(400);
      loginDTO.setMsg("密码不能为空");
      return loginDTO;
    } else {
      UserDTO userDTO = null;
      try {
        userDTO = meetingRoomClient.userLogin(user);
      } catch (Exception e) {
        log.error(e);
      }
      if(userDTO == null) {
        loginDTO.setCode(400);
        loginDTO.setMsg("用户名或密码错误");
        return loginDTO;
      } else {
        loginDTO.setCode(200);
        loginDTO.setMsg("登陆成功");
        loginDTO.setUser(userDTO);
        request.getSession().setAttribute("username",userDTO.getUsername());
        return loginDTO;
      }
    }
  }

  /**
   * 查询会议室列表
   * @return
   * @throws Exception
   */
  @RequestMapping(value = "/room/list", method = RequestMethod.POST)
  @ResponseBody
  public List<MeetingRoomDTO> getMeetingRoomList() throws Exception {
      List<MeetingRoomDTO> list = meetingRoomClient.getMeetingRoomList();
      if(list.size() == 0) {
          return null;
      }
      return list;
  }


  /**
   * 新增会议室
   * @return
   * @throws Exception
   */
  @RequestMapping(value = "/room/addRoom", method = RequestMethod.POST)
  @ResponseBody
  public ResultRoomDTO addRoom(@RequestBody MeetingRoomSaveParam meetingRoomSaveParam) throws Exception {
      ResultRoomDTO resultRoomDTO = new ResultRoomDTO();
      MeetingRoomDTO meetingRoomDTO = meetingRoomClient.saveMeetingRoom(meetingRoomSaveParam);
      if(meetingRoomDTO == null) {
         resultRoomDTO.setCode(400);
         resultRoomDTO.setMsg("保存失败");
      } else {
        resultRoomDTO.setCode(200);
        resultRoomDTO.setMsg("保存成功");
        resultRoomDTO.setMeetingRoomDTO(meetingRoomDTO);
      }
      return resultRoomDTO;
  }



  /**
   * 删除会议室
   * @param roomName
   * @return
   * @throws Exception
   */
  @RequestMapping(value = "/room/removeRoom", method = RequestMethod.GET)
  @ResponseBody
  public ResultDTO removeMeetingRoom(@RequestParam("roomName") String roomName) throws Exception {
      ResultDTO result = new ResultDTO();
      int code = meetingRoomClient.removeMeetingRoomByName(roomName);
      if(code > 0) {
          result.setCode(200);
          result.setMsg("删除成功");
      } else {
          result.setCode(400);
          result.setMsg("删除失败");
      }
      return result;
  }


  /**
   * 查询会议室预约详情列表
   * @return
   * @throws Exception
   */
  @RequestMapping(value = "/book/getBookDetailList", method = RequestMethod.GET)
  @ResponseBody
  public List<BookDetailDTO> getBookDetaillist() throws Exception {
    List<BookDetailDTO> list = meetingRoomClient.getBookDetailList();
    return list;
  }


  /**
   * 按会议室和指定日期查询预约详情
   * @param bookDetailQuery
   * @return
   * @throws Exception
   */
  @RequestMapping(method = RequestMethod.POST, value = "/book/getBookDetailByQuery")
  @ResponseBody
  public ResultBookDetailDTO getBookDetailByQuery(@RequestBody BookDetailQuery bookDetailQuery) throws Exception {
      ResultBookDetailDTO resultBookDetailDTO = new ResultBookDetailDTO();
      List<BookDetailDTO> bookDetailDTOList = meetingRoomClient.getBookDetailByQuery(bookDetailQuery);
      if(bookDetailDTOList == null) {
          resultBookDetailDTO.setCode(400);
          resultBookDetailDTO.setMsg("亲，您查询的会议室还没有预约哦！");
          resultBookDetailDTO.setBookDetailDTOList(null);
      } else {
          resultBookDetailDTO.setCode(200);
          resultBookDetailDTO.setMsg("亲，您查询的会议室已经有预约咯！");
          resultBookDetailDTO.setBookDetailDTOList(bookDetailDTOList);
      }
      return resultBookDetailDTO;

  }


  /**
   * 通过用户的id查询与用户相关的预约详情
   * @return
   * @throws Exception
   */
  @RequestMapping(value = "/book/getMyBookDetail", method = RequestMethod.GET)
  @ResponseBody
  public ResultBookDetailDTO getBookDetailByUserId(@RequestParam("id") Integer userId) throws Exception {
    ResultBookDetailDTO resultBookDetailDTO = new ResultBookDetailDTO();
    List<BookDetailDTO> bookDetailDTOList = meetingRoomClient.getBookDetailByUserId(userId);
    if(bookDetailDTOList == null) {
        resultBookDetailDTO.setCode(400);
        resultBookDetailDTO.setMsg("亲，您还没预约过会议室哦！");
        resultBookDetailDTO.setBookDetailDTOList(null);
    } else {
        resultBookDetailDTO.setCode(200);
        resultBookDetailDTO.setMsg("亲，您预约过以下会议室哦！");
        resultBookDetailDTO.setBookDetailDTOList(bookDetailDTOList);
    }
    return resultBookDetailDTO;
  }


  /**
   * 查询某个用户所属部门
   * @return
   * @throws Exception
   */
  @RequestMapping(value = "/user/getDepartment", method = RequestMethod.GET)
  @ResponseBody
  public Result<DepartmentDTO> getDepartmentById(@RequestParam("userId") Integer userId) throws  Exception {
    DepartmentDTO departmentDTO = null;
    if(userId == null) {
       throw new HttpMessageException(417,"参数不能为空");
    }
    try {
       departmentDTO = meetingRoomClient.getDepartmentByUserId(userId);

    } catch (Exception e) {
      log.error("所属部门查询失败，用户id为：{}", userId, e);
      Result.error("服务器错误");
    }
     return Result.success(departmentDTO);
  }


  /**
   * 查询某个会议室的设备
   * @param roomId
   * @return
   * @throws Exception
   */
  @RequestMapping(value = "/applyEquip/applyEquiplist", method = RequestMethod.GET)
  @ResponseBody
  public List<EquipDTO> getEquipByRoomId(@RequestParam("roomId") Integer roomId) throws  Exception {
    List<EquipDTO> equipDTOList = null;
    if(roomId == null) {
      throw new HttpMessageException(417,"参数不能为空");
    }
    equipDTOList = meetingRoomClient.getEquipByRoomId(roomId);
    if(equipDTOList.size() == 0) {
        return null;
    }
    return equipDTOList;
  }




  /**
   * 预约会议室
   * @param bookMeetingRoomParam
   * @return
   * @throws Exception
   */
  @RequestMapping(value="/book/bookMeetingRoom", method = RequestMethod.POST)
  @ResponseBody
  public ResultBookMeetingRoom bookMeetingRoom(@RequestBody BookMeetingRoomParam bookMeetingRoomParam) throws Exception {
    ResultBookMeetingRoom resultBookMeetingRoom = null;
    if(bookMeetingRoomParam == null) {
      throw new HttpMessageException(417,"参数不能为空");
    }
    resultBookMeetingRoom = meetingRoomClient.bookMeetingRoom(bookMeetingRoomParam);
    return resultBookMeetingRoom;
  }


  /**
   * 删除一条我的预约信息
   * @param bookId
   * @return
   * @throws Exception
   */
  @RequestMapping(method = RequestMethod.GET, value = "/books/deleteMyBook")
  public ResultDeleteMyBook deleteMyBook(@RequestParam("bookId") Integer bookId) throws Exception {
      if(bookId == null) {
          throw new HttpMessageException(417,"参数不能为空");
      }
      ResultDeleteMyBook resultDeleteMyBook = meetingRoomClient.deleteMyBook(bookId);
      return resultDeleteMyBook;
  }





}
